package sdmaker.parser;

import java.util.ArrayList;
import java.util.List;

import sdmaker.ast.AST;
import traces.Statement;

import fsa.State;
import fsa.Transition;

public class Utils {

	/* ************************** *
	 * *** GESTION DES LISTES *** *
	 * ************************** */
	
	public static List<AST> toListAST(List<ParseResult> parseResults){
		List<AST> asts=new ArrayList<AST>();

		for(ParseResult pr : parseResults){
			asts.add(pr.getResultingAST());
		}

		return asts;
	}
	
	public static <T> List<T> reverse(List<T> list){
		List<T> reversedList= new ArrayList<T>();

		if(list.size()> 0){
			for(int i=list.size()-1; i >= 0; i--){
				reversedList.add(list.get(i));
			}
		}
		
		return reversedList;
	}
	
	public static <T> T getLast(List<T> list){
		T result= null;

		if(list.size() > 0){
			result= list.get(list.size()-1);
		}

		return result;
	}
	
	public static <T> void removeLast(List<T> list){

		if(list.size() > 0){
			list.remove(list.size()-1);
		}
	}

	
	
	/* ************************** *
	 * ******** TESTEURS ******** *
	 * ************************** */
	
	public static boolean sameState(State st1, State st2){
		return (st1.toString().compareTo(st2.toString())==0);
	}
	
	public static boolean sameTransition(Transition t1, Transition t2){
		return (t1.toString().compareTo(t2.toString())==0);
	}
	
	public static boolean sameStatement(Statement stt1, Statement stt2){
		return (stt1.toString().compareTo(stt2.toString())==0);
	}
	
	public static boolean isClosingStatement(Statement statement){
		return statement.toString().contains("].closing_");
	}
	
	public static boolean isOpeningStatement(Statement statement){
		return !isClosingStatement(statement);
	}
}
